package com.hcbd.tdwt.base;

import android.util.Log;

import com.hcbd.tdwt.util.ConvertData;
import com.hcbd.tdwt.util.FileUtil;
import com.hcbd.tdwt.util.FileUtils;
import com.lrx.serialportlibrary.SerialPort;
import com.lrx.serialportlibrary.thread.SerialPortReadThread;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;

public abstract class SerialPortManage{
    protected SerialPort mSerialPort;

    public String logPath;
    public OutputStream mOutputStream;
    protected InputStream mInputStream;
    protected String TAG="SerialPortManage";
    public static boolean isThreadRun = true;
    private SerialPortReadThread mSerialPortReadThread;
    public  int baudRate = 115200;
    public SerialPortManage() {

    }
    public char byteToChar(byte byteIn) {
        switch(byteIn) {
            case 1:
                return '1';
            case 2:
                return '2';
            case 3:
                return '3';
            case 4:
                return '4';
            case 5:
                return '5';
            case 6:
                return '6';
            case 7:
                return '7';
            case 8:
                return '8';
            case 9:
                return '9';
            case 10:
                return 'A';
            case 11:
                return 'B';
            case 12:
                return 'C';
            case 13:
                return 'D';
            case 14:
                return 'E';
            case 15:
                return 'F';
            default:
                return '0';
        }
    }
    protected SerialPort getSerialPort(String sys_path,int baudrate) throws SecurityException, IOException, InvalidParameterException {
        Log.e("sys_path:",sys_path);
        if (this.mSerialPort == null) {
            this.mSerialPort = new SerialPort(new File(sys_path),baudrate, 0);
        }
        Log.e(TAG, "getSerialPort mSerialPort = " + this.mSerialPort);
        return this.mSerialPort;
    }

    public void openSerialPort(String sys_path,int baudrate) {
        try {
            this.mSerialPort = this.getSerialPort(sys_path,baudrate);
            this.mOutputStream = this.mSerialPort.getOutputStream();
            this.mInputStream = this.mSerialPort.getInputStream();
            startReadThread() ;
            Log.e(TAG, "openSerialPort success!!");
        } catch (Exception var2) {
            var2.printStackTrace();
            Log.e(TAG, "Serialport open error:" + var2);
        }
    }

    public void closeSerialPort() {
        this.isThreadRun = false;
        try {
            stopReadThread();
            if (this.mOutputStream != null) {
                this.mOutputStream.close();
                this.mOutputStream = null;
            }

            if (this.mInputStream != null) {
                this.mInputStream.close();
                this.mInputStream = null;
            }

            Log.e(this.TAG, "closeSerialPort success!!");
        } catch (Exception var2) {
            Log.e(this.TAG, "close Stream error:" + var2);
        }

        if (this.mSerialPort != null) {
            this.mSerialPort.close();
            this.mSerialPort = null;
        }

    }
  /*  protected abstract void onReceived(byte[] bytes);*/

    public void onDestroy() {
        this.mSerialPort = null;
    }



    private void startReadThread() {
        mSerialPortReadThread = new SerialPortReadThread(mInputStream) {
            @Override
            public void onDataReceived(byte[] bytes) {
                onReceived(bytes);
            }
        };
        mSerialPortReadThread.start();

    }
    /**
     * 停止接收消息的线程
     */
    private void stopReadThread() {
        if (null != mSerialPortReadThread) {
            mSerialPortReadThread.release();
        }
    }
    public void onReceived(byte[] bytes){
        try {
            String data = ConvertData.bytes2hex(bytes).toUpperCase();
            int index = data.indexOf("24");
            if (index == -1) {
                return;
            }
            if (index != 0) {
                data = data.substring(index);
            }
            index = data.indexOf("0D0A");
            while (index != -1) {
                String instruction = data.substring(0, index + 4);
                tailoringInstruction(ConvertData.hexToString(instruction));
                data = data.substring(index + 4);
                index = data.indexOf("0D0A");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    protected   void tailoringInstruction(String stringData){
        //查找校验和
        int xorIndex = stringData.indexOf("*");
        //<语句类型标识>, <数据字段1>,<数据字段2>, ..., <数据字段n>
        if (xorIndex != -1) {
            stringData = stringData.substring(0, xorIndex);
        }
        String []data=stringData.split("\\$BD");
        for(String str:data){
            onReceived(str);
        }
    }
     abstract public void  onReceived(String stringData);

}
